package com.dime.basic.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Optional;

import com.dime.basic.api.domain.DimeBaseharm;
import com.dime.basic.api.model.HarmType;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.core.utils.uuid.IdUtils;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.system.api.RemoteDictService;
import com.ruoyi.system.api.domain.SysDictData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.dime.basic.mapper.DimeBaseharmMapper;

import com.dime.basic.service.IDimeBaseharmService;

/**
 * 危害因素管理Service业务层处理
 *
 * @author Tiny
 * @date 2023-09-13
 */
@Service
public class DimeBaseharmServiceImpl implements IDimeBaseharmService
{
    @Autowired
    private DimeBaseharmMapper dimeBaseharmMapper;
    @Autowired
    private RemoteDictService dictService;
    /**
     * 查询危害因素管理
     *
     * @param id 危害因素管理主键
     * @return 危害因素管理
     */
    @Override
    public DimeBaseharm selectDimeBaseharmById(String id)
    {
        return dimeBaseharmMapper.selectDimeBaseharmById(id);
    }

    /**
     * 查询危害因素管理列表
     *
     * @param dimeBaseharm 危害因素管理
     * @return 危害因素管理
     */
    @Override
    public List<DimeBaseharm> selectDimeBaseharmList(DimeBaseharm dimeBaseharm)
    {
        return dimeBaseharmMapper.selectDimeBaseharmList(dimeBaseharm);
    }

    /**
     * 新增危害因素管理
     *
     * @param dimeBaseharm 危害因素管理
     * @return 结果
     */
    @Override
    public int insertDimeBaseharm(DimeBaseharm dimeBaseharm)
    {
        dimeBaseharm.setCreateTime(DateUtils.getNowDate());
        dimeBaseharm.setId(IdUtils.fastUUID());
        dimeBaseharm.setCreateBy(SecurityUtils.getUsername());
        return dimeBaseharmMapper.insertDimeBaseharm(dimeBaseharm);
    }

    /**
     * 修改危害因素管理
     *
     * @param dimeBaseharm 危害因素管理
     * @return 结果
     */
    @Override
    public int updateDimeBaseharm(DimeBaseharm dimeBaseharm)
    {
        dimeBaseharm.setUpdateTime(DateUtils.getNowDate());
        dimeBaseharm.setUpdateBy(SecurityUtils.getUsername());
        return dimeBaseharmMapper.updateDimeBaseharm(dimeBaseharm);
    }

    /**
     * 批量删除危害因素管理
     *
     * @param ids 需要删除的危害因素管理主键
     * @return 结果
     */
    @Override
    public int deleteDimeBaseharmByIds(String[] ids)
    {
        return dimeBaseharmMapper.deleteDimeBaseharmByIds(ids);
    }

    /**
     * 删除危害因素管理信息
     *
     * @param id 危害因素管理主键
     * @return 结果
     */
    @Override
    public int deleteDimeBaseharmById(String id)
    {
        return dimeBaseharmMapper.deleteDimeBaseharmById(id);
    }

    @Override
    public List<HarmType> selectAllHarmsGroupbyHarmType() {
        List<HarmType> harmTypes = dimeBaseharmMapper.selectAllHarmsGroupbyHarmType();
        R<List<SysDictData>> rdictData = dictService.getDictType("dime_harm_type");
        if(rdictData.getCode() != R.FAIL){
            List<SysDictData> dictData = rdictData.getData();
            if(StringUtils.isNotNull(dictData)) {
                for (HarmType h : harmTypes) {
                    Optional<SysDictData> oda = dictData.stream().filter(t -> t.getDictValue().equals(h.getHarmType())).findFirst();
                    if (oda.isPresent()) h.setHarmTypeName(oda.get().getDictLabel());
                }
            }
        }
        return harmTypes;
    }
    @Override
    public DimeBaseharm selectHarmByCode(String Code){
        return dimeBaseharmMapper.selectHarmByCode(Code);
    }
    @Override
    public List<DimeBaseharm> selectHarmsByCodes(String[] Codes){

        return dimeBaseharmMapper.selectHarmsByCodes(Codes);
    }
    @Override
    public List<DimeBaseharm> selectHarmsByIds(String[] Ids){
        return dimeBaseharmMapper.selectHarmsByIds(Ids);
    }
}
